/*
 * SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
 *
 * SPDX-License-Identifier: MulanPSL-2.0
 */
#![allow(unused_mut)]

use fluent_bundle::FluentArgs;
use fluent_resmgr::resource_manager::ResourceManager;
use std::ffi::CStr;
use sys_locale::get_locale;
use unic_langid::LanguageIdentifier;

/// # Safety
pub unsafe fn translate_one(mut tag: &str) -> String {
    let locale = get_locale().unwrap_or_else(|| String::from("en-US"));
    let langid: LanguageIdentifier = locale.parse().expect("wrong language");
    let locales = vec![langid];
    let resources = vec!["message.ftl".into()];

    let mgr =
        ResourceManager::new("/usr/share/locale/utsudo/translations/{locale}/{res_id}".into());
    let bundle = mgr.get_bundle(locales, resources);
    let value = bundle.get_message(tag).expect("Message doesn't exist.");
    let pattern = value.value().expect("Message has no value.");
    let mut errors = vec![];

    let mut msg1 = bundle.format_pattern(pattern, None, &mut errors);
    msg1.to_string()
}

/// # Safety
pub unsafe fn translate_two(mut tag: &str, mut string: *const libc::c_char) -> String {
    let locale = get_locale().unwrap_or_else(|| String::from("en-US"));
    let langid: LanguageIdentifier = locale.parse().expect("wrong language");
    let locales = vec![langid];
    let resources = vec!["message.ftl".into()];

    let mgr =
        ResourceManager::new("/usr/share/locale/utsudo/translations/{locale}/{res_id}".into());
    let bundle = mgr.get_bundle(locales, resources);
    let value = bundle.get_message(tag).expect("Message doesn't exist.");
    let pattern = value.value().expect("Message has no value.");
    let mut errors = vec![];
    let mut strings = FluentArgs::new();
    strings.set(
        "str1",
        //format!("{}", CStr::from_ptr(string).to_str().unwrap()),
        CStr::from_ptr(string).to_str().unwrap().to_string(),
    );

    let mut msg1 = bundle.format_pattern(pattern, Some(&strings), &mut errors);
    msg1.to_string()
}

/// # Safety
pub unsafe fn translate_three(
    mut tag: &str,
    mut string: *const libc::c_char,
    mut string2: *const libc::c_char,
) -> String {
    let locale = get_locale().unwrap_or_else(|| String::from("en-US"));
    let langid: LanguageIdentifier = locale.parse().expect("wrong language");
    let locales = vec![langid];
    let resources = vec!["message.ftl".into()];

    let mgr =
        ResourceManager::new("/usr/share/locale/utsudo/translations/{locale}/{res_id}".into());
    let bundle = mgr.get_bundle(locales, resources);
    let value = bundle.get_message(tag).expect("Message doesn't exist.");
    let pattern = value.value().expect("Message has no value.");
    let mut errors = vec![];
    let mut strings = FluentArgs::new();
    strings.set(
        "str1",
        //format!("{}", CStr::from_ptr(string).to_str().unwrap().to_string()),
        CStr::from_ptr(string).to_str().unwrap().to_string(),
    );
    strings.set(
        "str2",
        //format!("{}", CStr::from_ptr(string2).to_str().unwrap().to_string()),
        CStr::from_ptr(string2).to_str().unwrap().to_string(),
    );

    let mut msg1 = bundle.format_pattern(pattern, Some(&strings), &mut errors);
    msg1.to_string()
}

/// # Safety
pub unsafe fn translate_four(
    mut tag: &str,
    mut string: *const libc::c_char,
    mut string2: *const libc::c_char,
    mut string3: *const libc::c_char,
) -> String {
    let locale = get_locale().unwrap_or_else(|| String::from("en-US"));
    let langid: LanguageIdentifier = locale.parse().expect("wrong language");
    let locales = vec![langid];
    let resources = vec!["message.ftl".into()];

    let mgr =
        ResourceManager::new("/usr/share/locale/utsudo/translations/{locale}/{res_id}".into());
    let bundle = mgr.get_bundle(locales, resources);

    let value = bundle.get_message(tag).expect("Message doesn't exist.");
    let pattern = value.value().expect("Message has no value."); //
    let mut errors = vec![];
    let mut strings = FluentArgs::new();
    //unsafe {
    strings.set(
        "str1",
        //format!("{}", CStr::from_ptr(string).to_str().unwrap().to_string()),
        CStr::from_ptr(string).to_str().unwrap().to_string(),
    );
    strings.set(
        "str2",
        //format!("{}", CStr::from_ptr(string2).to_str().unwrap().to_string()),
        CStr::from_ptr(string2).to_str().unwrap().to_string(),
    );
    strings.set(
        "str3",
        //format!("{}", CStr::from_ptr(string3).to_str().unwrap().to_string()),
        CStr::from_ptr(string3).to_str().unwrap().to_string(),
    );
    //} //unsafe

    let mut msg1 = bundle.format_pattern(pattern, Some(&strings), &mut errors);
    msg1.to_string()
}
